<?php
declare (strict_types=1);

namespace app\listener\notify;

use app\event\MovieNotifySuccess;
use app\model\CouponOrder;
use app\model\MovieOrder;
use app\common\model\Sms;
use app\common\encryption\AlphaID;
use app\model\SmsLog;

class MovieOrderButtSuccess
{
    /**
     * 事件监听处理
     * @param MovieNotifySuccess $event
     * @return mixed
     */
    public function handle($event)
    {
        $couponOrder = $event->couponOrder;
        //判断订单状态
        if ($couponOrder->status != CouponOrder::C_STATUS_RECHARGE_SUCCESS) {
            return true;
        }
        $ext_data = $couponOrder->ext_data;

        //取票码信息
        if (empty($ext_data) || empty($ticket_codes = $ext_data['ticket_code'])) {
            return true;
        }
        /**
         * @var MovieOrder $movieOrder
         */
        //发送短信
        //手机号
        $mobile = $couponOrder->movieOrder->mobile;


        foreach (array_chunk($ticket_codes, 2, true) as $key => $ticket_codes) {
            //组装content
            $content = '';
            $num = 1;
//        $numArr = array('①', '②', '③ ', '④ ', '⑤ ', '⑥', ' ⑦ ', '⑧', ' ⑨ ', '⑩', '⑪', '⑫', '⑬', '⑭', '⑮', '⑯', '⑰', '⑱', '⑲', '⑳','㉑',
//            '㉒', '㉓', '㉔', '㉕', '㉖', '㉗', '㉘', '㉙', '㉚');
            $numArr = array('一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二', '十三', '十四', '十五', '十六',
                '十七', '十八', '十九', '二十', '二十一', '二十二', '二十三', '二十四', '二十五', '二十六', '二十七', '二十八', '二十九', '三十');
            foreach ($ticket_codes as $ticket_code) {
//                $content.= '票'.$numArr[$num-1].'：'.$ticket_code.'；';
                $content .= $ticket_code . '、';
                $num++;
            }
            //正式环境和测试环境不一样,犹豫测试环境的申请失败，现在共用一个
            if (isProd()) {
                $template = 'SMS_272595812';
            } else {
                $template = 'SMS_272520850';//SMS_248625020
            }
            $data = [
                'number' => count($ticket_codes),
                'content' => implode('、',$ticket_codes),
                'remark' => ':'.$couponOrder->movieOrder->cinema_name.','.$couponOrder->movieOrder->film_name,
                'time' => $couponOrder->movieOrder->show_time,
                'link' => '/c/' . alphaID($couponOrder->id),
            ];
            /*$result = $smsHandler->send([$mobile], [
                'template' => $template,
                'data' => $data,
            ]);*/

            //写入发送短信的表，定时任务发送
            /**
             * @var SmsLog $log
             */
            $log = SmsLog::query()->where('type', SmsLog::MOVIE_ORDER_TYPE)
                ->where('record_id', $couponOrder->id)
                ->where('mobile', json_encode([$mobile]))
                ->order('id desc')
                ->find();


            //已经发送成功或者正在等待发生的不再进行发送
            if ($log && in_array($log->status, [SmsLog::STATUS_WAIT, SmsLog::STATUS_SUCCESS]) && $key == 0) {
                return;
            }
            $row = SmsLog::create([
                    'type' => SmsLog::MOVIE_ORDER_TYPE,
                    'params' => json_encode([
                        'template' => $template,
                        'data' => $data,
                    ], JSON_UNESCAPED_UNICODE),
                    'mobile' => json_encode([$mobile]),
                    'record_id' => $couponOrder->id,
                    'result' => '',
                ]
            );
        }


        //dd($row);
    }
}
